package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Permission;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author Administrator
 * @Date: 2020/3/7 14:43
 * @Description:
 */
@Service
@Slf4j
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    PermissionDao permissionDao;

    @Override
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //开始分页,把pageNum,pageSize 封装成Page对象,存储到了当前线程中
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //开始条件查询,根据输入框输入的内容进行查询对应的方法
        //Page继承了ArrayList,说明Page本身就是一个List集合对象,还包含了分页参数
        Page<Permission> page= permissionDao.findByCondition(queryPageBean.getQueryString());

        //创建返回值,返回PageResult
        return new PageResult(page.getTotal(),page);
    }

    @Override
    public void delById(Integer id) {
        long count=permissionDao.findCountByPermissionId(id);
        if (count>0){
            throw new RuntimeException("该权限被角色关联,不能删除!!");
        }else {
            permissionDao.delById(id);
        }
    }



    @Override
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    @Override
    public void edit(Permission permission) {
        permissionDao.edit(permission);
    }


}
